<template>
	<view>
		<view>
			<coco-status-bar></coco-status-bar>
			<view class="padding bg-white flex align-center">
				<view class="flex-sub">COCONUT框架</view>
				<tui-icon name="shut" :size="26" @click="back"></tui-icon>
			</view>
		</view>
		<view class="text-xxl text-bold padding">登录</view>
		<view class="padding flex solid-bottom">
			<tui-icon name="people" color="#6d7a87" :size="20"></tui-icon>
			<input class="flex-sub margin-left-sm" :adjust-position="false" v-model="loginName" placeholder="请输入用户名" />
		</view>
		<view class="padding flex solid-bottom">
			<tui-icon name="pwd" color="#6d7a87" :size="20"></tui-icon>
			<input class="flex-sub margin-left-sm" :adjust-position="false" v-model="password" placeholder="请输入密码"
				:password="true" placeholder-class="tui-phcolor" type="text" maxlength="36" />
		</view>
		<view class="padding">
			<button :disabled="disabled" class="bg-blue lg" @tap="login">登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginName: '',
				password: '',
				disabled: true,
			};
		},
		watch: {
			loginName(n, o) {
				if (this.loginName && this.password) {
					this.disabled = false;
				} else {
					this.disabled = true;
				}
			},
			password(n, o) {
				if (this.loginName && this.password) {
					this.disabled = false;
				} else {
					this.disabled = true;
				}
			}
		},
		onLoad() {},
		methods: {
			back() {
				uni.navigateBack();
			},
			login() {
				this.$http.post("/cauth/sys/auth/login", {
					loginName: this.loginName,
					password: this.password
				}, res => {
					let data = res.data || {};
					if (data.token) {
						data['timestamp'] = new Date().getTime();
						uni.setStorageSync(this.$http.authInfoName(), data);
						uni.redirectTo({
							url: "/pages/index/index"
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: white;
	}
</style>
